﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarketOnlineDAO;
using MarketOnlineDTO;

namespace MarketOnlineBUS
{
    public class GioHangBUS
    {
        public Double TinhTongTienGioHang(List<GioHangDTO>list)
        {
            Double kq = 0;

            foreach(GioHangDTO item in list)
            {
                kq = kq + item.ThanhTien;
            }

            return kq;
        }

        public List<GioHangDTO> XoaSanPhamGioHang(List<GioHangDTO>list,int MaSP)
        {
            foreach(GioHangDTO item in list)
            {
                if(item.MaSP==MaSP)
                {
                    list.Remove(item);
                    break;
                }
            }

            return list;
        }


        public Double DemSoLuongSanPhamGioHang(List<GioHangDTO> list)
        {
            return list.Count;
        }


        public List<GioHangDTO> ThemSanPhamGioHang(List<GioHangDTO> list, int MaSP)
        {
            

            foreach (GioHangDTO item in list)
            {
                if (item.MaSP == MaSP)
                {
                    item.SoLuong = 1;
                    item.ThanhTien = item.DonGia * item.SoLuong;

                    return list;
                }
            }

            SanPhamDTO sp_dto = new SanPhamDTO();
            sp_dto = new SanPhamBUS().LaySanPham(MaSP);

            DonViTinhDTO dvt_dto = new DonViTinhDTO();
            dvt_dto = new DonViTinhBUS().LayDonViTinh(sp_dto.MaDonViTinh);


            GioHangDTO cart_dto = new GioHangDTO();
            cart_dto.HinhAnh = sp_dto.HinhAnh;
            cart_dto.TenSP = sp_dto.TenSP;
            cart_dto.DonGia = sp_dto.DonGia;
            cart_dto.MaSP = sp_dto.MaSP;
            cart_dto.SoLuong = 1;
            cart_dto.ThanhTien = sp_dto.DonGia;
            cart_dto.MaDonViTinh = sp_dto.MaDonViTinh;
            cart_dto.TenDonViTinh = dvt_dto.TenDonViTinh;
            cart_dto.MaLoaiSP = sp_dto.MaLoaiSanPham;

            list.Add(cart_dto);

            return list;
        }


        public List<GioHangDTO> CapNhatGioHang(List<GioHangDTO> list, int MaSP, Double SoLuong)
        {

            foreach (GioHangDTO item in list)
            {
                if (item.MaSP == MaSP)
                {
                    if (item.SoLuong == SoLuong)
                        break;

                    item.SoLuong = SoLuong;
                    item.ThanhTien = item.DonGia * item.SoLuong;
                    break;
                }
            }

            return list;
        }

        public Boolean KiemTra(List<GioHangDTO> list, int MaSP)
        {
            foreach (GioHangDTO item in list)
            {
                if (item.MaSP == MaSP)
                {
                    return true;
                }
            }

            return false;
        }
       
    }
}
